//////////////////////////////////////////////////////////////////////////
// FileName:Global
// CreateBy:DragonXiong
// CreateDate:2009-5-31
// Description:
//		Class Global
//      
// Modify Log:090602:Change Session Method --Dragon
//	          090625:Change Session End Method.Add function CheckForeLogout.--Mark
// Copyright:ASL-MIS
/////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Web;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.IO;
using Auto.Web.Common;
using Auto.Components;
using Auto.Data.Setting;
namespace BaseSystem
{
    public class Global : System.Web.HttpApplication
    {
        LoginLog logincurrent;

        protected void Application_Start(object sender, EventArgs e)
        {
            Log4net.InitLog4Net();
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// This function execute when the session start.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Session_Start(Object sender, EventArgs e)
        {
            //Session["skin"] = "Menu";
            //SessionBox.MenuStyle = "Menu";
            //Session["CurrentSystem"] = "_System";
            //Session["USER"] = "admin";
            //Session["CultureInfo"] = Settings.getValue("_System", "CultureInfo");
            //SessionBox.CreateCultureInfoSession(SysManager.SysCultureInfo);
            SessionBox.CultrueInfo = SysManager.SysCultureInfo;
        }

        /// <summary>
        /// This function execute when the session end.(When Session.Abandon() or Timeout) 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Session_End(Object sender, EventArgs e)
        {
            try
            {
                string userID = Session["UserID"].ToString();
                IDictionary<string, string> list = Application.Get("GLOBAL_USER_LIST") as IDictionary<string, string>;
                if (list != null)
                {
                    //Remove the user from "GLOBAL_USER_LIST" list of App.
                    list.Remove(new KeyValuePair<String, String>(userID, Session.SessionID));
                    Application.Lock();
                    Application["GLOBAL_USER_LIST"] = list;
                    Application.UnLock();

                    //If the user not forced logout,update the database.
                    if (!CheckForeLogout())
                    {
                            //Update user loginlog to the database.
                            logincurrent = new LoginLog();
                            logincurrent.UserID = userID;
                            logincurrent.LogoutTime = DateTime.Now;
                            logincurrent.Online = false;
                            logincurrent.LogoutType = "normal";
                            logincurrent.Logout();
                    }

                    //Remove forceLogout info from "ForceLogout" list of App.
                    IDictionary<string, string> list2 = Application.Get("ForceLogout") as IDictionary<string, string>;
                    list2.Remove(new KeyValuePair<String, String>(Session.SessionID, userID));
                    Application.Lock();
                    Application["ForceLogout"] = list2;
                    Application.UnLock(); 
                }


            }
            catch (Exception ex)
            {
                Response.Redirect("Error.aspx?Msg=" + ex.Message);
            }
           
        
        }

        /// <summary>
        /// Judge the user is forced logout or not from "ForceLogout" list of App.
        /// </summary>
        /// <returns>bool</returns>
        private bool CheckForeLogout()
        {
            string strUserId = Session["UserID"].ToString();
            IDictionary<string, string> list2 = Application.Get("ForceLogout") as IDictionary<string, string>;
            if (list2 != null)
            {
                foreach (KeyValuePair<string, string> keyValue in list2)
                {
                    if (strUserId == keyValue.Value)
                    {
                        return true;
                    } 
                }
                return false;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// Sets the culture based on the language selection in the settings.
        /// </summary>
        void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            //CultureInfo culture = CultureInfo.CreateSpecificCulture(Session["CultureInfo"].ToString());

            //Thread.CurrentThread.CurrentUICulture = culture;
            //Thread.CurrentThread.CurrentCulture = culture;
        }

        /// <summary>
        /// Application Error function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Application_Error(object sender, EventArgs e)
        {
            //Exception objErr = Server.GetLastError().GetBaseException();
            //string error = objErr.Message;
            //Server.ClearError();
            //Application["error"] = error;
            ////if(objErr.==-2147467259)
            //Response.Redirect("~/Error.aspx");
            ////Server.Transfer("~/Timeout.aspx");
        } 
    }
}